<template>
  <div class="message">
    <el-card>
      <!-- 搜索 -->
      <div class="search">
        <el-form :inline="true">
          <el-form-item label="发送者用户名：">
            <el-input
              v-model="params.sendUserName"
              clearable
              placeholder="请输入发送者用户名"
              @keyup.enter="handleQuery"
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" :loading="loading" @click="handleQuery">查询</el-button>
            <el-button :disabled="loading" @click="handleReset">重置</el-button>
          </el-form-item>
        </el-form>
      </div>

      <!-- 表格 -->
      <el-table :data="messageList" style="width: 100%" :loading="loading" stripe>
        <el-table-column prop="id" label="ID" width="80" />
        <el-table-column prop="title" label="标题" min-width="150" />
        <el-table-column prop="content" label="内容" min-width="200" />
        <el-table-column prop="sendUserName" label="发送人" width="120" />
        <el-table-column prop="receives" label="接收人" width="120" />
        <el-table-column prop="readCount" label="已读数" width="100" />
        <el-table-column prop="receiveUserCount" label="接受人数" width="100" />
        <el-table-column prop="createTime" label="创建时间" width="180" />
      </el-table>

      <div style="margin-top: 20px">
        <el-pagination
          v-model:current-page="params.pageIndex"
          v-model:page-size="params.pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-card>
  </div>
</template>

<script setup lang="ts">
import UserAPI from "@/api/system/user.api";
import { ref, onMounted, reactive } from "vue";

interface MessageType {
  id: number;
  title: string;
  content: string;
  sendUserName: string;
  receives: string;
  readCount: number;
  receiveUserCount: number;
  createTime: string;
}

const messageList = ref<MessageType[]>([]);
const total = ref(0);
const loading = ref(false);

// 查询参数
const params = reactive({
  sendUserName: undefined as string | undefined,
  pageIndex: 1,
  pageSize: 10,
});

// 获取消息列表
function getMessageList() {
  loading.value = true;
  UserAPI.getMessagePage(params)
    .then((data: any) => {
      messageList.value = data.list || [];
      total.value = data.total || 0;
    })
    .catch(() => {
      // ElMessage.error('获取消息列表失败');
    })
    .finally(() => {
      loading.value = false;
    });
}

function handleQuery() {
  params.pageIndex = 1;
  getMessageList();
}

function handleReset() {
  params.sendUserName = undefined;
  params.pageIndex = 1;
  getMessageList();
}

function handleSizeChange(size: number) {
  params.pageSize = size;
  params.pageIndex = 1;
  getMessageList();
}

function handleCurrentChange(page: number) {
  params.pageIndex = page;
  getMessageList();
}

onMounted(() => {
  getMessageList();
});
</script>

<style scoped lang="scss">
.message {
  padding: 16px;
}
</style>
